package com.wuseguang.springcloud.service.impl;


import com.wuseguang.springcloud.Classcardservice.ClassCardFieldService;
import com.wuseguang.springcloud.Classcardservice.ClassCardService;
import com.wuseguang.springcloud.dao.jdbc.ParentActivitiyJdbc;
import com.wuseguang.springcloud.service.ParentActivitiyService;
import com.wuseguang.springcloud.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ParentActivitiyServiceImpl implements ParentActivitiyService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ParentActivitiyJdbc activitiyJdbc;

    @Autowired
    private ClassCardService classCardService;

    @Autowired
    private ClassCardFieldService classCardFieldService;


    /**
     * @描述 查询家长签到活动(教师端)
     * @创建人 zm
     * @创建时间 2020/5/11
     */
    @Override
    public Result queryParentActivitiyList(Integer schoolId, Integer indexNo, Integer rows, String teacherId, String admin) {
        String sql = "SELECT p.name pname,p.id,s.name,s.class_icon,s.id classid,CONCAT(DATE_FORMAT(p.stime,'%Y.%m.%d %H:%i'),'-',DATE_FORMAT(p.etime,'%Y.%m.%d %H:%i')) atime,DATE_FORMAT(p.opertime,'%Y.%m.%d %H:%i') opertime,p.start,(SELECT COUNT(1) FROM student WHERE class_id=s.id) zcount,(SELECT COUNT(1) FROM student t,parent_activities_log g WHERE t.id=g.student_id AND g.parent_activities_id=p.id AND t.class_id=s.id) ycount FROM parent_activities p,classes  s  " +
                " WHERE p.class_id=s.id AND p.school_id=s.school_id AND p.school_id=?  ORDER BY p.opertime DESC";
        if (!"0".equals(admin)) {//不是管理员权限
            String classIds = activitiyJdbc.findClassIds(schoolId + "", teacherId);
            sql = "SELECT p.name pname,p.id,s.name,s.class_icon,s.id classid,CONCAT(DATE_FORMAT(p.stime,'%Y.%m.%d %H:%i'),'-',DATE_FORMAT(p.etime,'%Y.%m.%d %H:%i')) atime,DATE_FORMAT(p.opertime,'%Y.%m.%d %H:%i') opertime,p.start,(SELECT COUNT(1) FROM student WHERE class_id=s.id) zcount,(SELECT COUNT(1) FROM student t,parent_activities_log g WHERE t.id=g.student_id AND g.parent_activities_id=p.id AND t.class_id=s.id) ycount FROM parent_activities p,classes  s  " +
                    " WHERE p.class_id=s.id AND p.school_id=s.school_id AND p.school_id=? AND p.class_id IN (" + classIds + ")  ORDER BY p.opertime DESC";
        }
        List<Map<String, Object>> list = activitiyJdbc.queryListMap(sql, schoolId);
        //创建分页工具
        PagingTool tool = new PagingTool();
        //放入页码
        tool.setIndexNo(indexNo);
        //放入每页显示数量
        tool.setRows(rows);
        //放入角色集合
        tool.setList(list);
        PaginationUtil<?> paginationUtil = new PaginationUtil<>(list.size(), rows, tool.getList(), indexNo);
        //返回数据
        return new Result(paginationUtil);
    }


    /**
     * @描述 根据id查活动信息
     * @创建人 zm
     * @创建时间 2020/6/8
     */
    @Override
    public Result queryParentActivitiyById(Integer id) {
        String sql = "SELECT p.name pname,p.id,s.name,s.id classid,DATE_FORMAT(p.stime,'%Y-%m-%d %H:%i') stime,DATE_FORMAT(p.etime,'%Y-%m-%d %H:%i') etime,p.start FROM parent_activities p,classes  s   WHERE p.class_id=s.id AND p.school_id=s.school_id AND p.id=?  LIMIT 1 ";
        Map<String, Object> map = activitiyJdbc.queryMap(sql, id);
        //返回数据
        return new Result(map);
    }


    /**
     * @描述 添加家长活动(教师端)
     * @创建人 zm
     * @创建时间 2020/5/11
     */
    @Override
    public Result addParentActivitiy(Integer schoolId, Integer classId, String stime, String etime, String name, String state, String teacherId) {
        String sql = "insert into `parent_activities` (`school_id`, `class_id`, `name`, `stime`, `etime`, `opertime`, `state`,`teacher_id`) values(?,?,?,?,?,now(),?,?)";
        int i = activitiyJdbc.updateByParameter(sql, schoolId, classId, name, stime, etime, state, teacherId);
        if (i > 0) {
            return new Result();
        }
        //返回数据
        return new Result(CodeUtil.ADD_FAILURE);
    }

    /**
     * @描述 修改活动信息(教师端)
     * @创建人 zm
     * @创建时间 2020/5/11
     */
    @Override
    public Result updateParentActivitiy(Integer id, Integer classId, String stime, String etime, String name) {
        String sql = "update  `parent_activities` set `class_id`=?, `name`=?, `stime`=?, `etime`=? where id=?";
        int i = activitiyJdbc.updateByParameter(sql, classId, name, stime, etime, id);
        if (i > 0) {
            return new Result();
        }
        //返回数据
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }


    /**
     * @描述 修改活动开始/结束状态 0为 未开始 1为开始 展示在班牌(教师端)
     * @创建人 zm
     * @创建时间 2020/5/11
     */
    @Override
    public Result updateParentActivitiyStart(Integer id, Integer start, Integer classId) {
        String sql = "update  `parent_activities` set `start`=? where class_id=?";
        if (start == 1) {
            activitiyJdbc.updateByParameter(sql, 0, classId);
        }
        sql = "update  `parent_activities` set `start`=? where id=?";
        int i = activitiyJdbc.updateByParameter(sql, start, id);
        if (i > 0) {
            classCardFieldService.classesAndCards(classId, "meetingflag", start + "");
            classCardService.classesAndCards(classId);
            return new Result();
        }
        //返回数据
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 根据id删除活动信息(教师端)
     * @创建人 zm
     * @创建时间 2020/5/11
     */
    @Override
    public Result delParentActivitiy(Integer id) {
        String sql = " SELECT class_id ints FROM  parent_activities WHERE id=?";
        Integer integer1 = activitiyJdbc.queryInteger(sql, id);
        sql = "delete from  `parent_activities`  where id=?";
        int i = activitiyJdbc.updateByParameter(sql, id);
        if (i > 0) {
            classCardFieldService.classesAndCards(integer1 + "", "meetingflag", "0");
            return new Result();
        }
        //返回数据
        return new Result(CodeUtil.DELETE_FAILED);
    }

    /**
     * @描述 查看已到人数/或未到人数(教师端)
     * @创建人 zm
     * @创建时间 2020/5/11
     */
    @Override
    public Result queryActivitiyArrived(Integer id, Integer type, Integer indexNo, Integer rows) {//type 0 已到 1未到
        String sql = "SELECT t.id,t.student_name ,t.head_portrait,DATE_FORMAT(g.opertime,'%Y.%m.%d %H:%i') opertime,IF(g.opertime>p.stime,1,0)  state FROM parent_activities p,parent_activities_log g,student t WHERE p.id=g.parent_activities_id  AND t.id=g.student_id AND p.id=? GROUP BY t.id";
        if (type == 1) {
            sql = "SELECT t.id,t.student_name ,t.head_portrait,'' opertime,0 state,g.id  FROM (parent_activities p, student t) LEFT JOIN parent_activities_log g ON  (g.student_id=t.id AND p.id=g.parent_activities_id ) WHERE p.class_id=t.class_id AND p.id=? AND g.id IS NULL  GROUP BY t.id ";
        }
        List<Map<String, Object>> list = activitiyJdbc.queryListMap(sql, id);

        //创建分页工具
        PagingTool tool = new PagingTool();
        //放入页码
        tool.setIndexNo(indexNo);
        //放入每页显示数量
        tool.setRows(rows);
        //放入角色集合
        tool.setList(list);
        PaginationUtil<?> paginationUtil = new PaginationUtil<>(list.size(), rows, tool.getList(), indexNo);
        //返回数据
        return new Result(paginationUtil);
    }


    /**
     * @描述 根据学生id查询家长活动(家长端)
     * @创建人 zm
     * @创建时间 2020/5/11
     */
    @Override
    public Result queryActivitiyByStudentId(Integer studentId) {//学生id
        String sql = "SELECT p.name pname,p.id,s.name,s.class_icon,s.id classid,CONCAT(DATE_FORMAT(p.stime,'%Y.%m.%d %H:%i'),'-',DATE_FORMAT(p.etime,'%Y.%m.%d %H:%i')) atime,  DATE_FORMAT(p.opertime,'%Y.%m.%d %H:%i') opertime,IF((SELECT COUNT(1) FROM parent_activities_log WHERE  " +
                " parent_activities_id=p.id AND student_id=?)>0,1,0) state,(CASE WHEN p.stime>NOW() THEN 0 WHEN p.etime<NOW() THEN 1 ELSE 2 END ) type FROM parent_activities p,classes  s  ,student t WHERE p.class_id=s.id AND p.school_id=s.school_id AND  s.id=t.class_id AND t.id=?  ORDER BY p.opertime DESC";
        //返回数据
        return new Result(activitiyJdbc.queryListMap(sql, studentId, studentId));
    }


    /**
     * @描述 扫一扫，二维码签到
     * @创建人 zm
     * @创建时间 2020/5/11
     */
    @Override
    public Result addSigned(Integer id, String time, Integer studentId) {//学生id
        if (DateUtil.valueCompTime(DateUtil.gettime(), time) > 1) {//二维码已失效
            return new Result(CodeUtil.CODE_FAILURE);
        }
        String sql = "select count(1) ints from parent_activities_log where parent_activities_id=? and student_id=?";
        Integer integer = activitiyJdbc.queryInteger(sql, id, studentId);
        if (integer > 0) {//已签到
            return new Result(CodeUtil.SIGNED_IN);
        }
        sql = "insert into `parent_activities_log` (`parent_activities_id`, `student_id`, `opertime`) values(?,?,now())";
        int i = activitiyJdbc.updateByParameter(sql, id, studentId);
        if (i > 0) {//签到
            sql = "select class_id ints from student where id=?";
            Integer integer1 = activitiyJdbc.queryInteger(sql, studentId);
            classCardFieldService.classesAndCards(integer1 + "", "refreshflag", "1");
            classCardService.classesAndCards(integer1 + "");
            return new Result();
        }
        //返回数据
        return new Result(CodeUtil.ADD_FAILURE);
    }

}
